<?php
namespace Admin\Controller;
use Think\Controller;
class PrivilegeController extends CommonController {
    public function index()
    {
    	$privilege= D('Privilege');
    	$data=$privilege->priTree();
        $this->assign('data',$data);
        //var_dump($data);exit;
        $this->display();
    }

    public function add(){

    	$privilege= D('Privilege');
        if($_POST){
            if($privilege->create()){
                if($privilege->add()){
                    $this -> success('添加成功',U('index'));
                }else{
                    $this -> error('添加失败');
                }
            }else{
                $this -> error($privilege->getError());
                //exit($category->getError());
            }
            return;
        }

    	$data=$privilege->priTree();
        $this->assign('data',$data);
        $this->display();

    }
  public function edit()
    {

       $privilege= D('Privilege');
        if(IS_POST) {
            if($privilege->create()) {
                if($privilege->save()) {
                    $this->success('修改权限成功！',U('Privilege/index'));
                }else{
                    $this->error('修改权限失败！');
                }
            }else {
                $this->error($privilege->getError());
            }
            return;
        }
        $privilegeid=I('id');
        $info= $privilege->find($privilegeid);
        $this->assign('info',$info);
        $data=$privilege->priTree();
        $this->assign('data',$data);
        $this->display();

    }

    public function del()
    {
        $privilege= D('Privilege');
        $id=I('id');
        if($privilege->delete($id))
        {
            $this->success('成功删除权限及所属权限！',U('Privilege/index'));
        }else
        {
            $this->error('删除权限失败！');
        }
    }

    public function batchDel()
    {
        $privilege= D('Privilege');
        $ids=I('ids');
        $ids=implode(',', $ids);
        if($ids)
        {
            if($privilege->delete($ids))
            {
                $this->success('批量删除权限成功！',U('Privilege/index'));
            }else
            {
                $this->error('批量删除权限失败！');
            }
        }else
        {
            $this->error('未选中任何内容！');
        }
    }


}

